package com.grf.es.transport;

import java.net.UnknownHostException;
import java.util.Iterator;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.transport.TransportClient;

public class IndexQuery {

	/**
	 * 单个索引查询
	 * @throws UnknownHostException
	 */
	public void queryIndex() throws UnknownHostException {
		Index index = new Index();
		TransportClient client = index.getClient();
		GetResponse response = client.prepareGet("blog3", "article", "2").get();
		System.out.println(response.getSourceAsString());
		client.close();
	}
	
	/**
	 * 多个索引查询
	 * @throws UnknownHostException 
	 */
	public void queryMultiIndex() throws UnknownHostException {
		Index index = new Index();
		TransportClient client = index.getClient();
		MultiGetResponse response = client.prepareMultiGet().add("blog3", "article", "2")
			.add("blog3", "article", "1").get();
		Iterator<MultiGetItemResponse> iterator = response.iterator();
		while (iterator.hasNext()) {
			MultiGetItemResponse next = iterator.next();
			GetResponse response2 = next.getResponse();
			if (response2.isExists()) {
				System.out.println(response2.getSourceAsString());
			}
		}
	}
	
	public static void main(String[] args) throws UnknownHostException {
		IndexQuery q = new IndexQuery();
		//q.queryIndex(); //单个索引查询
		q.queryMultiIndex(); //多索引查询
	}
	
}
